﻿namespace OboutInc.Flyout2
{
    using System;
    using System.ComponentModel;
    using System.Web.UI;

    public class DragPanel : Control
    {
        private Flyout parent;
        private string realID;

        public DragPanel()
        {
            this.ViewState["draggingopacity"] = 100;
            this.ViewState["cursor"] = "move";
        }

        public Flyout findParentControl()
        {
            for (Control control = this.Parent; control.GetType().ToString().IndexOf("ASP") < 0; control = control.Parent)
            {
                if (control.GetType().ToString().IndexOf("Flyout2.Flyout") >= 0)
                {
                    return (Flyout) control;
                }
            }
            return null;
        }

        public string getClientID()
        {
            return this.realID;
        }

        protected override void OnPreRender(EventArgs e)
        {
            this.parent = this.findParentControl();
            if (this.parent != null)
            {
                this.realID = this.parent.registerDragPanel(this);
            }
            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.parent == null)
            {
                throw new InvalidOperationException("Drag Panel Must be inside a Flyout", null);
            }
            writer.Write("<div id=\"{0}\">", this.realID);
            base.Render(writer);
            writer.Write("</div>");
        }

        [Description("Gets or sets Cursor when mouse is over the DragPanel."), Category("Behavior")]
        public virtual string Cursor
        {
            get
            {
                return (string) this.ViewState["cursor"];
            }
            set
            {
                this.ViewState["cursor"] = value;
            }
        }

        [Category("Behavior"), Description("Gets or sets the Opacity of Flyout when it is dragged.")]
        public virtual int DraggingOpacity
        {
            get
            {
                return (int) this.ViewState["draggingopacity"];
            }
            set
            {
                this.ViewState["draggingopacity"] = value;
            }
        }
    }
}

